import mongoose, {SortOrder} from "mongoose";
import {UserModel} from "./models/User";
import  bcrypt from "bcrypt";
import {generateRandomString} from "./utli/generateAccessKeySecret";


const DATABASE_HOST = "xiongmingcai.mongodb.rds.aliyuncs.com";
const DATABASE_PORT = "3717";
const DATABASE_NAME = "mydatabase";
const DATABASE_USER = "myuser";
const DATABASE_PASS = process.env.DATABASE_PASS;

const DB_URL = `mongodb://${DATABASE_USER}:${DATABASE_PASS}@${DATABASE_HOST}:${DATABASE_PORT}/${DATABASE_NAME}`;
const run = async (...funcs: any) => {
    await mongoose.connect(DB_URL)

    for (const func of funcs) {
        await  func()
    }
    await mongoose.disconnect()
}

const createUser = async () => {
    const salt = await bcrypt.genSalt(10);
    console.log(salt);
    const hashedPassword = await bcrypt.hash("myPassword", salt);
    for (let i = 1; i < 34; i++) {
        const username = "xiongmc"+i;
        await  UserModel.create({
            username,
            email:username+"@example.com",
            hashedPassword,
            salt
        })
    }
}
const userLogin = async  ()=> {
    const  isPasswordMatch = await UserModel.comparePassword('xiong@example.com', 'myPassword');
    console.log(isPasswordMatch); // true
}
const userPages = async () => {
    const PAGE_SIZE = 10; // 每页显示的记录数
    const page = 4; // 当前页码
    const sortField = 'createdAt'; // 排序字段
    const sortOrder = 'ase'; // 排序方式

    const totalCount = await UserModel.countDocuments(); // 总记录数
    const totalPages = Math.ceil(totalCount / PAGE_SIZE); // 总页数


    const users = await UserModel.find().select("")

        .skip((page - 1) * PAGE_SIZE)
        .limit(PAGE_SIZE);
    console.log("总页数: " + totalPages);
    console.log("当前第几页: " + page);

    console.log(users); // 返回当前页按照创建时间倒序排序的用户数据
};

run(userPages)


